package gui.options;


import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;

import eclMain.ChatFile;
import eclMain.FileManager;
import gui.AutoCompletion;

public class ChannelOptionsPane extends JPanel {

	private static final long serialVersionUID = 1L;
	private TextManagerSettings settingsManager;

	public ChannelOptionsPane(TextManagerSettings settings) {
		this.settingsManager = settings;
		this.setLayout(new GridBagLayout()); //set the layout manager
		GridBagConstraints constraints = new GridBagConstraints();
		
		JPanel topRow = createTopRowGUI();
		
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.weightx = 1.0;
		constraints.weighty = 0.0;
		this.add(topRow, constraints);
		
		constraints.gridy = 1;
		constraints.weightx = 1.0;
		constraints.weighty = 1.0;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		this.add(createFollowedChannelList(), constraints);
	}
	
	private JPanel createFollowedChannelList() {
		//i need this to make sure that this panel adjusts it's layout according to how large it is
		//TODO: I would really prefer to have this as a scrollpane, but it doesn't seem to work right; maybe look into it later
		JPanel topPanel = new JPanel();
		new ChannelOptionsLayout(topPanel);
		
		for (int i = 0; i < 50; i++) {
			FollowedChannelPanel fcp2 = new FollowedChannelPanel("Test Label " + i);
			fcp2.setBorder(new EtchedBorder());
			topPanel.add(fcp2);
		}
		
		topPanel.setBorder(new LineBorder(Color.black));
		return topPanel;
	}
	
	private JPanel createTopRowGUI() {
		JPanel topRow = new JPanel();
		
		JButton addAllButton = createAddAllButton();
		JButton removeAllButton = createRemoveAllButton();
		JComboBox<String> recentChannelsBox = createRecentChannelsBox();
		JButton refreshButton = createRefreshButton(recentChannelsBox);
		JLabel addChannelLabel = new JLabel("Add Channel: ");
		
		FlowLayout layout = (FlowLayout) topRow.getLayout(); //I need to adjust the layout to get rid of any extra space, so that the border is closer to the components
		layout.setHgap(0);
		
		topRow.add(addAllButton);
		topRow.add(removeAllButton);
		topRow.add(new JLabel("  |  "));
		topRow.add(addChannelLabel);
		topRow.add(recentChannelsBox);
		topRow.add(refreshButton);
		
		return topRow;
	}
	
	private JComboBox<String> createRecentChannelsBox() {
		JComboBox<String> box = new JComboBox<String>();
		box.setEditable(true);
		box.setMaximumRowCount(8);
		AutoCompletion.enable(box);
		return box;
	}

	private JButton createRefreshButton(final JComboBox<String> recentChannelsBox) {
		JButton button = new JButton("Refresh");
		button.setToolTipText("Refresh the list of recent channels");
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				TreeSet<String> channelList = new TreeSet<String>();
				ArrayList<ChatFile> fileList = FileManager.getMasterList();
				for (ChatFile file : fileList) channelList.add(file.getChannel().getChannelName());
				//TODO: remove channel names that are already listed as being followed
				
				recentChannelsBox.removeAllItems(); //get rid of the old list
				for (String channelName : channelList) recentChannelsBox.addItem(channelName); //refresh with the new list
			}
		});
		return button;
	}
	
	private JButton createAddAllButton() {
		JButton button = new JButton("Add All");
		button.setToolTipText("Add all recently active channels");
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				settingsManager.followAllChannels();
				//TODO: update the UI in this panel with all channels
			}
		});
		
		return button;
	}
	
	private JButton createRemoveAllButton() {
		JButton button = new JButton("Remove All");
		button.setToolTipText("Remove all listed channels");
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				settingsManager.unfollowAllChannels();
			}
		});
		
		return button;
	}
}
